---
category: Validator
created: '2020-06-04'
title: Check if a number is in a given range
updated: '2021-10-13'
---

**JavaScript version**

```js inRange.js
const inRange = (num, a, b, threshold = 0) => Math.min(a, b) - threshold <= num && num <= Math.max(a, b) + threshold;
```

**TypeScript version**

```ts inRange.ts
const inRange = (num: number, a: number, b: number, threshold: number = 0): boolean =>
    Math.min(a, b) - threshold <= num && num <= Math.max(a, b) + threshold;
```

**Examples**

```js examples.js
inRange(10, 5, 15); // true
inRange(10, 5, 6); // false
inRange(10, 15, 5); // true
inRange(-10, -5, -15); // true
```
